package cn.apthink.controller.system;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.apthink.controller.BaseResult;
import cn.apthink.controller.DataResult;
import cn.apthink.controller.ListResult;
import cn.apthink.model.ARole;
import cn.apthink.model.ARolePermission;
import cn.apthink.model.VRoleMenu;
import cn.apthink.model.URole;
import cn.apthink.mybatisplus.entity.RoleEntity;
import cn.apthink.mybatisplus.entity.RolePermissionEntity;
import cn.apthink.mybatisplus.entity.UserRoleEntity;
import cn.apthink.mybatisplus.service.RolePermissionService;
import cn.apthink.mybatisplus.service.RoleService;
import cn.apthink.mybatisplus.service.UserRoleService;
import cn.apthink.service.AuthService;
import cn.apthink.solon.Logging;
import cn.apthink.utility.ObjectUtil;
import cn.apthink.utility.Util;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 角色
 *
 * @author David唐
 * @createdate 2024年1月18日
 */
@Logging
@Slf4j
@Controller
@Api(value="角色控制器")
@Mapping("/api/role")
public class RoleController {
	@Inject
	RoleService roleService;
	@Inject
	UserRoleService userRoleService;
	@Inject
	RolePermissionService rolePermissionService;
	@Inject
	AuthService authService;

	@Mapping("/delete")
	@ApiOperation("删除指定角色信息")
	@SaCheckPermission("role#delete")
	public BaseResult delete(@ApiParam(name = "id", value = "角色id", required = true) String id) {
		RoleEntity role = roleService.getById(id);
		if (Util.isEmpty(role)) {
			return DataResult.error(99, "角色id不存在", "roleid");
		}
		QueryWrapper<UserRoleEntity> q = new QueryWrapper<>();
		q.eq("roleid", id);
		userRoleService.remove(q);
		QueryWrapper<RolePermissionEntity> q2 = new QueryWrapper<>();
		q2.eq("roleid", id);
		rolePermissionService.remove(q2);
		roleService.removeById(id);
		return  DataResult.success("删除角色成功",role);
	}

	@Mapping("/add")
	@ApiOperation("新增角色信息")
	@SaCheckPermission("role#add")
	public BaseResult add(@ApiParam(name = "ARole", value = "角色信息", required = true) ARole data) {
		if (Objects.isNull(data.getRolename())) {
			return DataResult.error(101, "角色名不能为空", "rolename");
		}
		RoleEntity role = new RoleEntity();
		ObjectUtil.copy(data, role);
//		David: 采用MyBatisPlus自动填充功能
//		role.setId(Utils.guid());
//		role.setCreatetime(DateUtil.now());
		roleService.save(role);
		return DataResult.success("新增角色成功",role);
	}

	@Mapping("/update")
	@ApiOperation("修改角色信息")
	@SaCheckPermission("role#update")
	public BaseResult update(@ApiParam(name = "URole", value = "角色信息", required = true) URole data) {
		log.debug("update", data);
		if (Objects.isNull(data.getId())) {
			return DataResult.error(101, "id不能为空", "id");
		}
		if (Objects.isNull(data.getRolename())) {
			return DataResult.error(101, "角色名不能为空", "rolename");
		}
		RoleEntity role = roleService.getById(data.getId());
		if (Objects.isNull(role)) {
			return DataResult.error(99, "角色id不存在", "roleid");
		}
		ObjectUtil.copy(data, role);
//		David: 采用MyBatisPlus自动填充功能
//		role.setUpdatetime(DateUtil.now());
		roleService.updateById(role);
		return DataResult.success("变更角色成功", role);
	}

	@Mapping("/permission")
	@ApiOperation("修改角色权限信息")
	@SaCheckPermission("role#permission")
	public BaseResult permission(@Body @ApiParam(name = "ARolePermission", value = "角色权限信息", required = true) ARolePermission rolePermission) {
		log.debug(rolePermission.toString());
		if (Objects.isNull(rolePermission.getRoleid())) {
			return DataResult.error(101, "角色id不能为空", "roleid");
		}
		RoleEntity role = roleService.getById(rolePermission.getRoleid());
		if (Objects.isNull(role)) {
			return DataResult.error(99, "角色id不存在", "roleid");
		}
		if (Objects.isNull(rolePermission.getMenupermissions())) {
			return DataResult.error(101, "菜单权限对象集不能为null", "menupermissions");
		}
		List<RolePermissionEntity> result = authService.saveRolePermission(rolePermission.getRoleid(), rolePermission.getMenupermissions());
		return ListResult.success(result);
	}

	@Mapping("/defaultdata")
	@ApiOperation("缺省角色信息")
	@SaCheckPermission("role#query")
	public ListResult<RoleEntity> defaultdata() {
		log.debug("defaultdata");
		List<String> dd = Arrays.asList("planners", "managers");
		List<RoleEntity> ds = roleService.listByIds(dd);
		ListResult<RoleEntity> result = new ListResult<>();
		result.setData(ds);
		return result;
	}

	@Mapping("/all")
	@ApiOperation("所有角色信息")
	@SaCheckPermission("role#query")
	public ListResult<RoleEntity> all() {
		log.debug("all");
		List<RoleEntity> ds = roleService.list();
		return ListResult.success(ds);
	}

	@Mapping("/rolemenu")
	@ApiOperation("指定角色的菜单信息")
	@SaCheckPermission("role#query")
	public BaseResult rolemenu(@ApiParam(name = "roleid", value = "角色id", required = true) String roleid) {
		log.debug(roleid);
		if (!Objects.isNull(roleid)) {
			if (Objects.isNull(roleService.getById(roleid))) {
				return DataResult.error(99, "角色id不存在", "roleid");
			}
		}
		List<VRoleMenu> ds = rolePermissionService.selectQRoleMenu(roleid);
		return ListResult.success(ds);
	}

}